import { fetchAdminMyCarList } from '../../../../services/good/fetchGoods';
import request from '../../../../utils/requestAdmin';

Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    swiperImageProps: { mode: 'scaleToFill' },
    userType: 1,
    shopId: 0,
    flag: -1,
    marquee1: {
      speed: 80,
      loop: -1,
      delay: 0,
    },
    userInfo: {
    }
  },

  goodListPagination: {
    pageNo: 1,
    pageSize: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  onShow() {
    var userInfo = getApp().globalData.admin;
    console.log("sss", userInfo)
    this.setData({
      userInfo: userInfo.user
    })
    this.init();
  },

  onLoad() {
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadGoodsList(true);
  },

  tabChangeHandle(e) {
    this.privateData.tabIndex = e.detail;
    this.loadGoodsList(true);
  },

  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({ goodsListLoadStatus: 1 });

    const pageSize = this.goodListPagination.pageSize;
    let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      var nextData = {
        rows: []
      }
      console.log("current usertype =", this.data.userType)
      nextData = await fetchAdminMyCarList(pageIndex, pageSize);
      console.log("nextlist", nextData)
      this.setData({
        goodsList: fresh ? nextData.rows : this.data.goodsList.concat(nextData.rows),
        goodsListLoadStatus: 0,
      });
      this.goodListPagination.pageNo = pageIndex;
      this.goodListPagination.pageSize = pageSize;
    } catch (err) {
      this.setData({ goodsListLoadStatus: 3 });
    }
  },
  goodListClickHandle(e) {
    const { goods } = e.detail;
    getApp().globalData.currentAuction = goods;
    wx.navigateTo({
      url: `/pages/goods/details/index?id=${goods.id}&type=${goods.type}&status=${goods.status}`,
    });
  },

  onEdit(e) {
    const { goods } = e.detail;
    getApp().globalData.currentCar = goods;
    wx.navigateTo({
      url: '/pages/admin/usercenter/editcar/index?id=' + goods.id
    });
  },
  onDel(e) {
    const { goods } = e.detail;
    console.log("g", goods)
    var that = this;
    wx.showModal({
      content: '是否删除车辆',
      success(res) {
        if (res.confirm) {
          that.submitDelCar(goods.id);
        }
      }
    })
  },
  submitDelCar(id) {
    var that = this;
    var url = "car/admin/stock/delStock?id=" + id
    request.post(url, {}, function (res) {
      if (res.code == 0) {
        that.loadGoodsList(true);
      } else {
        wx.showModal({ title: '提示', content: res.message, showCancel: false });
      }
    })
  }
});
